package com.sunagy.demand.front.controller.api;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.sunagy.demand.front.VO.ResultVO;
import com.sunagy.demand.front.enums.BuzEnum;
import com.sunagy.demand.front.exception.BuzException;
import com.sunagy.demand.front.service.SmsService;
import com.sunagy.demand.front.utils.ResultVOUtil;
import com.sunagy.demand.front.utils.UUIDUtil;
import com.sunagy.demand.front.utils.ValidUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * desc：#TODO fill me ！
 * author：三餘無夢生
 * date：2018/4/27
 * time：下午2:35
 */
@Slf4j
@RestController
@RequestMapping("/common/verifyCode")
@Api(value = "通用验证码", description = "获取手机验证码")
public class VerifyCodeApi extends BaseApi {

    @Autowired
    private SmsService smsService;

    @ApiOperation(value = "获取6位验证码", notes = "通过手机号获取验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "java.lang.String", name = "Authorization", value = "令牌信息", required = false, paramType = "header"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "接口异常")
    })
    @RequestMapping(value = "/{phone}", method = RequestMethod.POST)
    public ResultVO<SendSmsResponse> verifyCode(@PathVariable("phone") String phone) {
        if(!ValidUtil.phoneNumber(phone)){
            throw new BuzException(BuzEnum.PARAMS_INCORRECT);
        }
        String verifyCode = UUIDUtil.createUUid(6);
        HashMap<String,String> attrMap = new HashMap<>();
        attrMap.put("code",verifyCode);
        SendSmsResponse sendSmsResponse = smsService.send(phone, "SMS_133920125", attrMap, verifyCode);
        return ResultVOUtil.success(sendSmsResponse);
    }
}
